﻿using System;
using System.Data;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;


namespace HRM
{
    public partial class frmUNCList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int CurrPage = 0;
        int toltalPage = 0;
        int itemCount = 0;
        int _UNCID = 0;
        UNCClient _wcfUNC;
        RoleRightInfo ListRole;
        public frmUNCList()
        {
            InitializeComponent();
            //LoadLanguage();
            _wcfUNC = Service.UNC();
            ListRole = UIParamater.Quyen_UNC;
            if (!ListRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }
            BuildVoucherGrid();
            LoadData();
            this.KeyPreview = true;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
            {
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
                UpdateUI.Load_ToolStrip(ref toolStrip1);
                UpdateUI.Load_ContextMenuStrip(ref mnuPopUp);
            }
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "ROWNUMBER", "STT", "C", 70, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_ID", "Mã Chính", "C", 100, false, true);
            UISetting.AddColumn(ref dgvList, "UNC_No", "Số phiếu", "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_Date", "Ngày tháng", "L", 150, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_Quater", "Quý", "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_Month", "Tháng", "R", 100, true, true);           
            UISetting.AddColumn(ref dgvList, "UNC_Year", "Năm", "R", 100, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_BranchCode", "Mã chi nhánh", "C", 120, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_Content", "Nội dung", "l", 150, true, true);
            UISetting.AddColumn(ref dgvList, "UNC_Amount", "Số tiền", "R", 120, true, true);           
            UISetting.AddColumn(ref dgvList, "UNC_Description", "Mô tả", "L", 200, true, true);
            
        }
        void LoadData()
        {
            DataSet ds = _wcfUNC.UNC_GetList(out itemCount, CurrPage, pageSize);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);

                dgvList.Rows[i].Cells["ROWNUMBER"].Value = ds.Tables[0].Rows[i]["ROWNUMBER"].ToString();
                dgvList.Rows[i].Cells["UNC_ID"].Value = ds.Tables[0].Rows[i]["UNC_ID"].ToString();
                dgvList.Rows[i].Cells["UNC_No"].Value = ds.Tables[0].Rows[i]["UNC_No"].ToString();
                dgvList.Rows[i].Cells["UNC_Quater"].Value = ds.Tables[0].Rows[i]["UNC_Quater"].ToString();                
                dgvList.Rows[i].Cells["UNC_Month"].Value =Convert.ToInt32(ds.Tables[0].Rows[i]["UNC_Month"].ToString());
                if (ds.Tables[0].Rows[i]["UNC_Date"].ToString()!= "")
                    dgvList.Rows[i].Cells["UNC_Date"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["UNC_Date"]).ToString("dd/MM/yyyy");
                else
                    dgvList.Rows[i].Cells["UNC_Date"].Value = "";
                dgvList.Rows[i].Cells["UNC_Year"].Value = Convert.ToInt32(ds.Tables[0].Rows[i]["UNC_Year"].ToString());
                dgvList.Rows[i].Cells["UNC_Amount"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["UNC_Amount"].ToString()).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["UNC_BranchCode"].Value = ds.Tables[0].Rows[i]["UNC_BranchCode"].ToString();
                dgvList.Rows[i].Cells["UNC_Content"].Value = ds.Tables[0].Rows[i]["UNC_Content"].ToString(); ;
                dgvList.Rows[i].Cells["UNC_Description"].Value = ds.Tables[0].Rows[i]["UNC_Description"].ToString();
            }
        }
        private void PageIndex()
        {
            toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (toltalPage == 0)
                lblPageIndex.Text = "0" + "/" + toltalPage.ToString();
            else
                lblPageIndex.Text = (CurrPage + 1).ToString() + "/" + toltalPage.ToString();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!ListRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmUNCInfo(0, ListRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                CurrPage = 0;
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            if (dgvList.SelectedRows.Count > 0)
            {
                if (ListRole.Update || ListRole.View)
                {
                    _UNCID = Convert.ToInt32(dgvList.CurrentRow.Cells["UNC_ID"].Value);
                    Form f = new frmUNCInfo(_UNCID, ListRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!ListRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _UNCID = Convert.ToInt32(dgvList.CurrentRow.Cells["UNC_ID"].Value);

                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfUNC.UNC_Delete(_UNCID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }

        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            CurrPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (CurrPage != 0)
                CurrPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (toltalPage > 0 && CurrPage != toltalPage - 1)
                CurrPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (toltalPage != 0)
                CurrPage = toltalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnEdit_Click(null, null);
            }
        }

        private void frmUNCList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopUp.Enabled = true;
                    btnNewBranch.Enabled = true;
                    btnEditBranch.Enabled = true;
                }
            }
        }

        private void btnNewBranch_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditBranch_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void frmUNCList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            //if (dgvList.Columns.Count > 0)
            //{
            //    dgvList.Columns["UNC_Name"].HeaderText = UpdateUI.GetString("phucapluong");
            //}
        }

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}